from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.remote.webdriver import WebDriver

from base.base_page import BasePage
from page.search_page import SearchPage


class SmartPracticeSetPage(BasePage):
    """
    选择学科页
    """
    # 关键词
    _keyword = (AppiumBy.XPATH,
                "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[1]/android.view.View/android.widget.EditText")
    # 题目类型
    # - 不限
    _questions_type_all = (AppiumBy.XPATH,
                           "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[2]/android.view.View/android.widget.RadioButton[1]")
    # - 选择题
    _questions_type_select = (AppiumBy.XPATH,
                              "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[2]/android.view.View/android.widget.RadioButton[2]")
    # - 案例题
    _questions_type_case = (AppiumBy.XPATH,
                            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[2]/android.view.View/android.widget.RadioButton[3]")
    # 答题情况
    # - 不限
    _answer_in_all = (AppiumBy.XPATH,
                      "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[3]/android.view.View/android.widget.RadioButton[1]")
    # - 未做题
    _answer_in_unfinished = (AppiumBy.XPATH,
                             "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[3]/android.view.View/android.widget.RadioButton[2]")
    # - 已做题
    _answer_in_finished = (AppiumBy.XPATH,
                           "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.widget.ListView/android.view.View[3]/android.view.View/android.widget.RadioButton[3]")

    # 正确率：滑动选择，直接点击元素默认51%
    # 试题数目：滑动选择，直接点击元素默认51个
    # 提交按钮
    _submit_button = (AppiumBy.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.RelativeLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]")

    def to_practice_page(self):
        """
        步骤：选择“中级”——“数据库系统工程师”
        :return: 首页
        """
        self.driver.implicitly_wait(3)
        # 录入关键词“软件测试”
        self.find_element(self._keyword).send_keys("软件测试")
        # 选择题目类型、答题情况
        self.find_element_and_click(self._questions_type_select)
        self.find_element_and_click(self._answer_in_unfinished)
        # 滑动设置正确率与试题数目
        TouchAction(self.driver).tap(self.sds, 50, 50, count=1)
        TouchAction(self.driver).press(x=560, y=628).move_to(x=790, y=628).release().perform()
        TouchAction(self.driver).press(x=500, y=760).move_to(x=700, y=760).release().perform()
        self.find_element_and_click(self._submit_button)
        return self
